//引入请求封装
import {
    request
} from "../../request/request.js"; //ES6 promise
// 引入SDK核心类
// var QQMapWX = require('/utils/qqmap-wx-jssdk');
var QQMapWX = require('../../utils/qqmap-wx-jssdk')
// 实例化API核心类
var qqmapsdk = new QQMapWX({
    key: 'XCNBZ-UOS3G-XRIQL-ICAKK-JZIOK-EUB27' // 必填
});
// pages/toPush/toPush.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        img: '../../image/',
        // 手机号
        iphone: '',
        // 密码开关
        switch1: true,
        // 密码值
        password: '',
        // 赏金开关
        switch2: true,
        // 人数开关
        switch3: true,
        // 起始时间显示
        start: '',
        // 起始时间设置
        startDate: '',
        // 结束时间显示
        end: '',
        // 结束时间设置
        startDate: '',
        // 步骤列表
        step: [{
            id: 0,
            name: '',
            detail: '',
            address: '',
            jing: '0.00',
            wei: '0.00'
        }],
        // 定位城市
        city: '',
        cityStatus: 0
    },
    Data: {
        token: '',      //用户凭证
        taskName: '',   //任务名称
        taskDetail: '', //任务描述
        iphone: '',     //联系方式
        password: {     //密码设置
            valid: true,
            value: ''
        },
        money: {        //赏金设置
            valid: true,
            value: 5
        },
        memberSum: {    //人数设置
            valid: true,
            value: 1
        },
        startTime: '',  //起始时间
        endTime: '',    //结束时间
        step: '',       //步骤信息
        city: ''        //任务发布地点
    },
    // 活动名称
    taskName(e) {
        let name = e.detail.detail.value
        this.Data.taskName = name
    },
    // 活动简介
    taskDetail(e) {
        let detail = e.detail.detail.value
        this.Data.taskDetail = detail
    },
    // 获取手机号
    inputIphone(e) {
        let that = this
        let iphone = e.detail.detail.value
        var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        if (!myreg.test(iphone)) {
            wx.showToast({
                icon: 'error',
                title: '手机号码不正确',
                success() {
                    that.setData({
                        iphone: ''
                    })
                }
            })
        } else {
            this.setData({
                iphone: iphone
            })
            this.Data.iphone = iphone
        }
    },
    // 密码开关切换
    onChangePassword(event) {
        const detail = event.detail;
        this.setData({
            'switch1': detail.value
        })
        this.Data.password.valid = detail.value
    },
    setPassword(e) {
        let password = e.detail.value
        this.setData({
            password: password
        })
        this.Data.password.value = password
    },
    // 赏金开关切换
    onChangeMoney(event) {
        const detail = event.detail;
        this.setData({
            'switch2': detail.value
        })
        this.Data.money.valid = detail.value
    },
    setMoney(e) {
        let money = e.detail.value
        this.Data.money.value = money
    },
    // 人数开关切换
    onChangeMember(event) {
        const detail = event.detail;
        this.setData({
            'switch3': detail.value
        })
        this.Data.memberSum.valid = detail.value
    },
    setMember(e) {
        let memberSum = e.detail.value
        this.Data.memberSum.value = memberSum
    },
    // 设置当前日期
    getNowFormatDate() {
        let date = new Date();
        let seperator1 = "-";
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        let currentdate = year + seperator1 + month + seperator1 + strDate;
        this.setData({
            startDate: currentdate,
            endDate: currentdate,
            start: currentdate,
            end: currentdate
        })
        this.Data.startTime = currentdate
        this.Data.endTime = currentdate
    },
    // 选择日期
    startDate(e) {
        this.setData({
            start: e.detail.value,
            end: e.detail.value,
            endDate: e.detail.value
        });
        this.Data.startTime = e.detail.value
        this.Data.endTime = e.detail.value
    },
    endDate(e) {
        this.setData({
            end: e.detail.value
        });
        this.Data.endTime = e.detail.value
    },
    // 添加步骤
    addStep() {
        let step = this.data.step
        let id = step.length
        if (step[id - 1].name == '' || step[id - 1].detail == '' || step[id - 1].address == '') {
            wx.showToast({
                icon: 'error',
                title: '请先完善该步骤!',
                duration: 2000,
            })
        } else {
            let num = (Number)(step[step.length - 1].id) + 1
            let temp = {
                id: num,
                name: '',
                detail: '',
                address: '',
                jing: '0.00',
                wei: '0.00'
            }
            step.push(temp)
            this.setData({
                step: step
            })
        }
    },
    // 删除步骤
    removeStep(e) {
        if (this.data.step.length == 1) {
            wx.showToast({
                icon: 'error',
                title: '请至少保留一项',
            })
            return
        }
        let id = e.currentTarget.dataset.id
        let step = this.data.step
        for (let i = 0; i < step.length; i++) {
            if (step[i].id == id) {
                step.splice(i, 1)
                break;
            }
        };
        this.setData({
            step: step
        })
    },
    stepName(e) {
        let id = e.currentTarget.dataset.id
        let name = e.detail.detail.value
        let step = this.data.step
        step.forEach(i => {
            if (i.id == id)
                i.name = name
        });
        this.setData({
            step: step
        })
    },
    stepDetail(e) {
        let id = e.currentTarget.dataset.id
        let detail = e.detail.detail.value
        let step = this.data.step
        step.forEach(i => {
            if (i.id == id)
                i.detail = detail
        });
        this.setData({
            step: step
        })
    },
    stepAddress(e) {
        let id = e.currentTarget.dataset.id
        let address = e.detail.value
        let step = this.data.step
        step.forEach(i => {
            if (i.id == id)
                i.address = address
        });
        this.setData({
            step: step
        })
    },
    // 提交表单
    submit() {
        this.Data.step = this.data.step
        let length = this.Data.step.length - 1
        let that = this
        // 名称校验
        if (that.Data.taskName == '') {
            wx.showToast({
                icon: 'none',
                title: '请输入活动名称 !',
            })
        }
        // 简介校验
        else if (that.Data.taskDetail == '') {
            wx.showToast({
                icon: 'none',
                title: '请输入活动简介 !',
            })
        }
        // 联系校验
        else if (that.Data.iphone == '') {
            wx.showToast({
                icon: 'none',
                title: '请输入联系方式 !',
            })
        }
        // 步骤校验
        else if (that.Data.step[length].name == "" || that.Data.step[length].detail == "" || that.Data.step[length].address == "") {
            wx.showToast({
                icon: 'none',
                title: '请完善步骤信息 !',
            })
        }
        // 定位校验
        else if (that.Data.city == '') {
            wx.showToast({
                icon: 'none',
                title: '请进行定位 !',
            })
        } else {
            // 密码校验
            if (that.Data.password.valid == true) {
                if (that.Data.password.value == '') {
                    wx.showToast({
                        icon: 'none',
                        title: '请输入密码 !',
                    })
                } else {
                    console.log(that.Data)
                    that.postForm()
                    return
                }
            }
            console.log(that.Data)
            that.postForm()
        }
    },
    // 发布任务
    async postForm() {
        let that = this
        const res = await request({
            url: "/pushTask",
            data: that.Data,
            method: "post"
        })
        if (res.status == 200 && res.tip == 'success') {
            console.log(res)
            wx.showToast({
                title: '任务发布成功',
                duration: 2000,
                icon: 'success',
                success() {
                    wx.redirectTo({
                        url: '../task/task',
                    })
                }
            })
        }
    },
    // 选择地点
    chooseAddress(e) {
        let id = e.currentTarget.dataset.id
        let step = this.data.step
        let that = this
        wx.chooseLocation({
            success(res) {
                console.log(res)
                step.forEach(i => {
                    if (i.id == id) {
                        i.address = res.name;
                        if (res.name == "")
                            i.address = "自定义地点"
                        i.jing = res.longitude;
                        i.wei = res.latitude
                    }
                });
                that.setData({
                    step: step
                })
            }
        })
    },
    // 任务定位
    dingwei() {
        let that = this
        wx.getLocation({
            type: 'gcj02',
            success(res) {
                let latitude = res.latitude
                let longitude = res.longitude
                qqmapsdk.reverseGeocoder({
                    sig: 'XCNBZ-UOS3G-XRIQL-ICAKK-JZIOK-EUB27', // 必填
                    location: {
                        latitude,
                        longitude
                    },
                    success(res) {
                        let province = res.result.address_component.province
                        let city = res.result.address_component.city
                        let district = res.result.address_component.district
                        that.setData({
                            city: province + " " + city + " " + district,
                            cityStatus: 1
                        })
                        that.Data.city = province + " " + city + " " + district
                    },
                    fail(err) {
                        wx.showToast('获取城市失败')
                    }
                })
            },
            fail() {
                wx.showToast({
                    icon: 'none',
                    title: '请开启手机定位',
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        wx.getStorage({
            key: 'token',
            success(res) {
                that.Data.token = res.data
            }
        })
        this.getNowFormatDate()
    },
})